﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.IO;
using System.Threading;

namespace ZMenFM.Ramification._2TTPlayerList
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            ZMenFM.Utils.UI.Aero.ZMAeroHelper.ExtendGlassFrame(this, new Thickness(-1));
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            ZMenFM.Network.Album.AlbumItem ai = new Network.Album.AlbumItem(textBox1.Text);

            while (ai.GetLoadStatus() == Network.Album.AlbumLoadStatus.ALS_LOADING) Thread.Sleep(10);
            if (ai.GetLoadStatus() == Network.Album.AlbumLoadStatus.ALS_LOAD_FAILED)
            {
                MessageBox.Show("生成失败。可能是地址错误或者网络不通畅。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            String str = ai.ToString();

            String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n";
            xml += ("<ttplaylist title=\"[" + ai.GetAlbumName() + "]\" version=\"4\" generator=\"ZMenFM 2 TTPlayerList\">\n");
            xml += ("<format tagtitle=\"%A - %T\" deftitle=\"%F\"/>\n");

            String items = "";
            int cnt = 0;
            List<ZMenFM.Network.Song.SongItem> songs = ai.GetSongList();
            for (int i = 0; i < songs.Count; i++)
            {
                if (songs[i].GetFirstItem() != null)
                {
                    cnt++;
                    items += ("<item file=\"" + songs[i].GetFirstItem().SongUrl + "\" title=\"" + songs[i].GetFirstItem().SongUrl + "\" />\n");
                }
            }

            xml += "<items count=\"" + cnt.ToString() + "\">\n";
            xml += items;
            xml += "</items>\n</ttplaylist>\n";

            StreamWriter sw = File.CreateText("ttlist.ttpl");
            sw.WriteLine(xml);
            sw.Close();

            MessageBox.Show("创建完成！不过基于这只是个Demo，很多歌可能听不了。");
        }
    }
}
